package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.Distribute;
import com.zyd.shiro.business.service.PakDistributeService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakDistribute;
import com.zyd.shiro.persistence.mapper.PakDistributeMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakDistributeServiceImpl implements PakDistributeService {

    @Autowired
    private PakDistributeMapper pakDistributeMapper;

    @Override
    public Distribute insert(Distribute distribute) {
        Assert.notNull(distribute, "对象不可为空！");
        distribute.getPakDistribute().setUnicode("PF"+System.currentTimeMillis());
        distribute.getPakDistribute().setAppstatus(0);
        distribute.getPakDistribute().setDisstatus(0);
        distribute.getPakDistribute().setClaimstatus(0);
        distribute.getPakDistribute().setUpdateTime(new Date());
        distribute.getPakDistribute().setCreateTime(new Date());
        pakDistributeMapper.insertSelective(distribute.getPakDistribute());
        return distribute;
    }

    @Override
    public void insertList(List<Distribute> distributes) {
            for(Distribute distribute:distributes){
               distribute.getPakDistribute().setUpdateTime(new Date());
               distribute.getPakDistribute().setCreateTime(new Date());
               pakDistributeMapper.insertSelective(distribute.getPakDistribute());
           }
    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakDistributeMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(Distribute distribute) {
        Assert.notNull(distribute, "对象不可为空！");
        distribute.setUpdateTime(new Date());
        return pakDistributeMapper.updateByPrimaryKey(distribute.getPakDistribute()) > 0;
    }

    @Override
    public boolean updateSelective(Distribute distribute) {
        Assert.notNull(distribute, "对象不可为空！");
        distribute.setUpdateTime(new Date());
        return pakDistributeMapper.updateByPrimaryKeySelective(distribute.getPakDistribute()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public Distribute getByPrimaryKey(Long primaryKey) {
        PakDistribute pakDistribute = pakDistributeMapper.selectByPrimaryKey(primaryKey);
        return null == pakDistribute ? null : new Distribute(pakDistribute);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public Distribute getOneByEntity(Distribute test) {
        Assert.notNull(test, "对象不可为空！");
        PakDistribute pakDistribute = pakDistributeMapper.selectOne(test.getPakDistribute());
        return null == pakDistribute ? null : new Distribute(pakDistribute);
    }

    @Override
    public List<Distribute> listAll() {
        List<PakDistribute> pakDistributes = pakDistributeMapper.selectAll();
        if (CollectionUtils.isEmpty(pakDistributes)) {
            return null;
        }
        List<Distribute> distributes = new ArrayList<>();
        for (PakDistribute pakDistribute : pakDistributes) {
            distributes.add(new Distribute(pakDistribute));
        }
        return distributes;
    }

    @Override
    public List<Distribute> listByEntity(Distribute entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<Distribute> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakDistribute> pakDistributes = pakDistributeMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakDistributes)) {
            return null;
        }
        List<Distribute> distributes = new ArrayList<>();
        for (PakDistribute pakDistribute : pakDistributes) {
            distributes.add(new Distribute(pakDistribute));
        }
        PageInfo bean = new PageInfo<PakDistribute>(pakDistributes);
        bean.setList(distributes);
        return bean;
    }
    @Override
    public void increase(PakDistribute pakDistribute) {
        pakDistributeMapper.insert(pakDistribute);
    }




}
